﻿using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Component.UI
{
    public class TransferTools
    {

        private static readonly Color[] _color =
        {
            Color.red,
            Color.blue,
        };

        public static List<DrawMeshTools.MeshType> CurrentType
        {
            get;
            set;
        } = new List<DrawMeshTools.MeshType>();

        public static DrawMeshTools.MeshType RandomMeshType(DrawMeshTools.MeshType currentType)
        {
            int random = Random.Range(0, CurrentType.Count);

            if (CurrentType[random] == currentType)
            {
                random = (random + 1) % CurrentType.Count;
                
            }
            return CurrentType[random];
        }

        public static readonly int MaxColor = _color.Count();
        private static int[] _colorsAvailable = new int[MaxColor];
        public static int ColorIndex { get; set; }

        public static Color Color
        {
            get
            {
                return _color[ColorIndex];
            }
            set
            {
                if (value == Color.red)
                {
                    ColorIndex = 0;
                }else if (value == Color.blue)
                {
                    ColorIndex = 1;
                }
            }
        }

        public static int ColorsIndex(Color color)
        {
            for (int i = 0; i < _color.Length; i++)
            {
                if (color == _color[i])
                {
                    return i;
                }
            }

            return -1;
        }

        public static bool SwitchColor()
        {
            if (_colorsAvailable[ColorIndex] == 0)
            {
                return true;
            }
            else
            {
                if (ColorIndex + 1 < MaxColor && _colorsAvailable[ColorIndex + 1] == 0)
                {
                    ColorIndex += 1;
                    return true;
                }
            }
            return false;
        }

        public static void BeAvailable(Color color)
        {
            if (color == Color.red)
            {
                ColorIndex = 0;
            }else if (color == Color.blue)
            {
                ColorIndex = 1;
            }
            _colorsAvailable[ColorIndex] = 0;
        }

        public static void BeNotAvailable()
        {
            _colorsAvailable[ColorIndex] = 1;
        }
        
    }
}